package com.example.blackbox.Adapter;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.viewpager.widget.ViewPager;

import com.bumptech.glide.Glide;
import com.example.blackbox.Data.News;
import com.example.blackbox.R;

import java.util.ArrayList;

public class NewsAdapter extends BaseAdapter {
    ArrayList<News> news=new ArrayList<>();
    Context view;

    public NewsAdapter(ArrayList<News> news, Context view) {
        this.news = news;
        this.view=view;
    }

    @Override
    public int getCount() {
        return news.size();
    }

    @Override
    public Object getItem(int position) {
        return news.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;

        //Log.d("testposition",position+"");
        if (position==0){
            News data=news.get(position);
            convertView=LayoutInflater.from(parent.getContext()).inflate(R.layout.item_news_banner,null);
            ViewPager viewPager=convertView.findViewById(R.id.vp);
            BannerAdapter bannerAdapter=new BannerAdapter();
            bannerAdapter.setData(data.getBanner());
            viewPager.setAdapter(bannerAdapter);
        }else{
            convertView= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_news,null);
            viewHolder=new ViewHolder();
            viewHolder.newsTitle=convertView.findViewById(R.id.news_title);
            viewHolder.newsTag=convertView.findViewById(R.id.news_tag);
            viewHolder.newsDate=convertView.findViewById(R.id.news_date);
            viewHolder.newsMessage=convertView.findViewById(R.id.news_message);
            viewHolder.newsImg=convertView.findViewById(R.id.news_img);

            News data=news.get(position);
            viewHolder.newsTitle.setText(data.getNewsTitle());
            viewHolder.newsTag.setText(data.getNewsTag());
            viewHolder.newsDate.setText(data.getNewsDate());
            viewHolder.newsMessage.setText(data.getNewsMessage());

            Glide.with(view).load(data.getImgUrl()).into(viewHolder.newsImg);

        }

        return convertView;
    }

    class ViewHolder{
        TextView newsTitle,newsTag,newsMessage,newsDate;
        ImageView newsImg;
    }
}
